package site.brood.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.brood.user.pojo.entity.User;
import site.brood.user.service.IPermissionService;
import site.brood.user.service.IUserService;

import java.util.HashSet;
import java.util.Set;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private IUserService userService;

    /**
     * 获取用户所属角色列表
     *
     * @param userId 用户Id
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(Long userId) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        // 不用再添加额外角色
        if (userService.isAdmin(userId))
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(userService.selectRolesByUserId(userId));
        }
        return roles;
    }

    /**
     * 获取菜单权限列表
     *
     * @param userId 用户Id
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(Long userId){
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (userService.isAdmin(userId))
        {
            perms.add("*:*:*");
        }
        else
        {
            perms.addAll(userService.selectMenuPermsByUserId(userId));
        }
        return perms;
    }

}
